package com.jmedemos;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;



import com.jme.app.*;
import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.math.Vector3f;
import com.jme.scene.shape.*;
import com.jme.light.DirectionalLight;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.model.XMLparser.XMLtoBinary;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.util.ModelLoader;
import com.jme.scene.Node;
import com.jme.scene.Spatial;



public class CarScene extends SimpleGame {
//	public Node loadXML(String filename){
//		String XMLFileName = "MyFileName.xml";
//		URL modelURL = ModelLoader.class.getClassLoader().getResource(XMLFileName);
//		XMLtoBinary converter = new XMLtoBinary();
//		JmeBinaryReader jbr = new JmeBinaryReader();
//		jbr.setProperty("bound", "box");
//		ByteArrayOutputStream BO = new ByteArrayOutputStream();
//		Node modelNode = null;
//		try {
//		    converter.sendXMLtoBinary(modelURL.openStream(), BO);
//		    modelNode = jbr.loadBinaryFormat(new ByteArrayInputStream(BO.toByteArray()));
//		} catch (IOException ex) {
//		    ex.printStackTrace();
//		}
//		return modelNode;
//	}
	
	public static Spatial loadModel (String modelFile){
		Spatial			loadedModel	= null;
		FormatConverter		formatConverter = null;		
		ByteArrayOutputStream 	BO 		= new ByteArrayOutputStream();
		String			modelFormat 	= modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
		String			modelBinary	= modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
		URL			modelURL	= ModelLoader.class.getClassLoader().getResource(modelBinary);
 
		//verify the presence of the jbin model
		if (modelURL == null){
 
			modelURL		= ModelLoader.class.getClassLoader().getResource(modelFile);
 
			//evaluate the format
			if (modelFormat.equals("3ds")){
				formatConverter = new MaxToJme();
			
			} else if (modelFormat.equals("obj")){
				formatConverter = new ObjToJme();
				formatConverter.setProperty("mtllib", modelURL);
			}
			//formatConverter.setProperty("mtllib", modelURL);
 
			try {
				formatConverter.convert(modelURL.openStream(), BO);
				loadedModel = (Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
 
				//save the jbin format
				BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
			} catch (IOException e) {				
				e.printStackTrace();
				return null;
			}
		}else{
			try {
				//load the jbin format
				loadedModel = (Spatial) BinaryImporter.getInstance().load(modelURL.openStream());
			} catch (IOException e) {
				return null;
			}
		}
 
		return loadedModel;
	}
	@Override
	protected void simpleInitGame() {
		// TODO Auto-generated method stub
		//Box box = new Box("myBox", new Vector3f(), 1, 1, 1);
		//box.setModelBound(new BoundingBox());
		// Point to a URL of my model
		Spatial car = loadModel("jmetest/data/model/Car_Williams/f1car.jbin");//("jmetest/data/model/Grand/GPE.obj");//
		//Spatial map = loadModel("jmetest/data/model/racetrack/MountainValley_Track_Raceway.obj");
		//map.setLocalScale(.005f);
		car.setLocalScale(50.0f);
		rootNode.attachChild(car);
		//rootNode.attachChild(map);
	}
	public static void main(String[] args){
		CarScene game = new CarScene();
		game.setConfigShowMode(ConfigShowMode.AlwaysShow);
		game.start();
	}

}
